package com.example.myapplication01;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.Arrays;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView rv;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recycler_view);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.rv), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        rv = findViewById(R.id.rv_view);

        // 设置线性布局
        /*LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // 设置item线性水平排列方式
        rv.setLayoutManager(layoutManager);*/

        // 设置网格布局
        /*GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        rv.setLayoutManager(gridLayoutManager);*/

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        rv.setLayoutManager(staggeredGridLayoutManager);


        rv.setAdapter(new LinearAdapter(this));
        rv.addItemDecoration(new MyDecoration());
    }

    class MyDecoration extends RecyclerView.ItemDecoration {


        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int size = getResources().getDimensionPixelSize(R.dimen.dividerHeight);
            outRect.set(size, size, size, size);
        }
    }

    static class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {

        private final Context mContext;
        private final List<String> mData = Arrays.asList("Hello", "World", "Small", "Cow", "Big",
                "Small", "Cow", "Big", "Small", "Cow", "Big", "Small", "Cow", "Big", "Small", "Cow",
                "Big", "Small", "Cow", "Big", "Small", "Cow", "Big", "Small");

        public LinearAdapter(Context mContext) {
            this.mContext = mContext;
        }

        @NonNull
        @Override
        public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new LinearViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_rv_item, parent, false));
        }


        @Override
        public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, int position) {
            /*holder.tv.setText(mData.get(position));
            holder.tv.setOnClickListener(v -> Toast.makeText(mContext, "pos:" + position, Toast.LENGTH_SHORT).show());*/

            if (position % 2 != 0) {
                holder.iv.setImageResource(R.mipmap.image_1);
            } else {
                holder.iv.setImageResource(R.mipmap.image_2);
            }

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }


        static class LinearViewHolder extends RecyclerView.ViewHolder {

            private TextView tv;
            private ImageView iv;

            public LinearViewHolder(@NonNull View itemView) {
                super(itemView);
                // tv = itemView.findViewById(R.id.tv_title);
                iv = itemView.findViewById(R.id.imageView);
            }
        }


    }
}